package com.heima.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author stone
 * @description 针对表【ad_sensitive(敏感词信息表)】的数据库操作Service实现
 * @createDate 2023-02-22 10:27:50
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive>
        implements AdSensitiveService {


    @Override
    public ResponseResult findAllSensitives(SensitiveDTO dto) {
        // 分页查询
        dto.checkParam();
        Integer dtoPage = dto.getPage();
        Integer dtoSize = dto.getSize();
        Page<AdSensitive> sensitivePage = new Page<>(dtoPage, dtoSize);

        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery().like(StringUtils.isNotEmpty(dto.getName()), AdSensitive::getSensitives, dto.getName());
        IPage<AdSensitive> page = this.page(sensitivePage, wrapper);
        return new PageResponseResult(dtoPage, dtoSize, page.getTotal(), page.getRecords());
    }

    @Transactional
    @Override
    public ResponseResult saveSensitives(SensitiveDTO sensitiveDTO) {
        if (Objects.isNull(sensitiveDTO) || Objects.isNull(sensitiveDTO.getSensitives()) || sensitiveDTO.getSensitives().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能空，长度不能大于10");
        }
        // 校验是否已经存在敏感词
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitiveDTO.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        AdSensitive adSensitive = new AdSensitive();
        BeanUtils.copyProperties(sensitiveDTO, adSensitive);
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult updateSensitives(SensitiveDTO sensitiveDTO) {
        if (Objects.isNull(sensitiveDTO)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 根据id 查找敏感词、根据内容查找是否已经存在敏感词，不存在，修改
        if (Objects.isNull(sensitiveDTO.getSensitives()) || sensitiveDTO.getSensitives().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能空，长度不能大于10");
        }

        if (sensitiveDTO.getSensitives().length() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (Objects.isNull(sensitiveDTO.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer sensitiveId = sensitiveDTO.getId();
        String dtoSensitives = sensitiveDTO.getSensitives();
        AdSensitive adSensitive = this.getById(sensitiveId);
        if (Objects.isNull(adSensitive)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (!adSensitive.getSensitives().equals(dtoSensitives)) {
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, dtoSensitives));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        adSensitive.setSensitives(dtoSensitives);
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteSensitives(Integer id) {
        if (Objects.isNull(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId, id));
        if (count == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }
}




